package com.lkm.app_ui;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android_serialport_api.SerialPort;

public abstract class SerialPortManager {
    protected InputStream mInputStream;
    protected OutputStream mOutputStream;
    protected ReadThread mReadThread;
    protected SerialPort mSerialPort;

    public class ReadThread extends Thread {
        public void run() {
            super.run();
            while (!isInterrupted()) {
                try {
                    byte[] buffer = new byte[64];
                    if (SerialPortManager.this.mInputStream != null) {
                        int size = SerialPortManager.this.mInputStream.read(buffer);
                        if (size > 0) {
                            SerialPortManager.this.onDataReceived(buffer, size);
                        }
                    } else {
                        return;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                }
            }
        }
    }

    protected abstract void initPort();

    protected abstract void onDataReceived(byte[] bArr, int i);

    public SerialPortManager() {
        initPort();
    }

    public ReadThread getReadThread() {
        return this.mReadThread;
    }

    public OutputStream getOutputStream() {
        return this.mOutputStream;
    }
}